package cn.dyp.com.getnotification.activity;

import android.support.v7.app.AppCompatActivity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import cn.dyp.com.getnotification.dialog.IpSetDialog;
import cn.dyp.com.getnotification.service.NotifiService;
import cn.dyp.com.getnotification.R;

/**
 * 获取通知的主控制类
 *
 * @author asus
 *
 */
//没有获取button实例，也没有按钮的点击事件，而是直接在activity_main.xml中设置android:onClick
public class MainActivity extends AppCompatActivity {
    private IpSetDialog mDialog;
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        initDialog();
        initIntent();
    }

    /**
     * 初始化Intent
     *
     */
    private void initIntent() {
        mIntent = new Intent(MainActivity.this, NotifiService.class);
    }

    /**
     * 初始化Ip设置Dialog
     *
     */
    private void initDialog() {
        mDialog = new IpSetDialog(this);
    }

    /**
     * 弹出Ip设置对话框
     *
     * @param v
     *            View
     */
    public void setIp(View v) {
        mDialog.show(getFragmentManager(), "dialog");
    }

    /**
     * 开启Service获取通知
     *
     * @param v
     *            View
     */
    public void getNotifi(View v) {
        startService(mIntent);
    }

    /**
     * 停止Service
     *
     * @param v
     *            View
     */
    public void stop(View v) {
        stopService(mIntent);
    }

    /**
     * 清除所有通知
     *
     * @param v
     *            View
     */
    public void clear(View v) {
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancelAll();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(mIntent);
    }
}
